/**  
 * All rights Reserved, Designed By Soywer   
 * @Title:  ActionDirective.java   
 * @Package com.demo.core.freemarker.tags   
 * @Description:    TODO(用一句话描述该文件做什么)   
 * @author: Soywer     
 * @date:   2016年7月18日 下午2:00:53   
 * @version V1.0     
 */  
package com.demo.core.freemarker.tags;

import java.io.IOException;
import java.util.Map;

import com.demo.common.util.StringUtils;
import com.demo.core.config.ITitle;
import com.demo.core.statics.Constant;

import freemarker.core.Environment;
import freemarker.template.ObjectWrapper;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

/**   
 * @ClassName:  ActionDirective   
 * @Description:TODO(自定义通过Action输出title/meta标签)   
 * @author: Soywer  
 * @date:   2016年7月18日 下午2:00:53   
 *      
 */
public class ActionDirective implements  TemplateDirectiveModel {

	/**   
	 * <p>Title: execute</p>   
	 * <p>Description: </p>   
	 * @param environment
	 * @param map
	 * @param atemplatemodel
	 * @param templatedirectivebody
	 * @throws TemplateException
	 * @throws IOException   
	 * @see freemarker.template.TemplateDirectiveModel#execute(freemarker.core.Environment, java.util.Map, freemarker.template.TemplateModel[], freemarker.template.TemplateDirectiveBody)   
	 */  
	@Override
	public void execute(Environment env, Map params, TemplateModel[] loopVars,
			TemplateDirectiveBody body) throws TemplateException, IOException {
		String action = null;
		
		String title = null;
		
		String meta = null;
		//写到配置文件里；通过action查询对应的title、meta等
		if(params.containsKey(Constant.ACTION)&&StringUtils.isNotBlank(params.get(Constant.ACTION))){
			String actionvalue = params.get(Constant.ACTION).toString();
			String titleKey = actionvalue + "." + Constant.ACTION_TITLE_PREFIX;
			String metaKey = actionvalue + "." + Constant.ACTION_META_PREFIX;
			
			title = ITitle.get(titleKey);
			meta = ITitle.get(metaKey);
			if(StringUtils.isBlank(title)){
				title = ITitle.get(Constant.ACTION_DEFAULT_TITLE);
			}
			if(StringUtils.isBlank(meta)){
				meta = ITitle.get(Constant.ACTION_DEFAULT_META);
			}
			
			env.setVariable("layoutTitle",ObjectWrapper.DEFAULT_WRAPPER.wrap(title));
			env.setVariable("layoutMeta",ObjectWrapper.DEFAULT_WRAPPER.wrap(meta));
		}
		
		body.render(env.getOut());
		
	}

}
